(function () {

    layui.use(['laydate', 'table', 'layer', 'form', 'code', 'jquery', 'element', 'upload'], function () {

        var table = layui.table, layer = layui.layer, form = layui.form, $ = layui.jquery, element = layui.element, upload = layui.upload;

        /**
         * 【公共区】==================================================================================================
         */

        var _web_speech_api_manage = {};

        /**
         * 【table 定义区】==================================================================================================
         */
        _web_speech_api_manage.table = {};

        _web_speech_api_manage.table.templet = {};

        _web_speech_api_manage.table.templet.xxx = function(data) {
            return data.xxx;
        };

        _web_speech_api_manage.table.option = {};

        _web_speech_api_manage.table.option.init = {
        };

        _web_speech_api_manage.table.option.add = {
        };

        _web_speech_api_manage.table.option.import = {
        };

        _web_speech_api_manage.table.option.edit = {
        };

        _web_speech_api_manage.table.option.send = {
        };

        _web_speech_api_manage.table.option.delete = {
        };


        /**
         * 【table 方法区】==================================================================================================
         */
        _web_speech_api_manage.table.fn = {};

        // init
        _web_speech_api_manage.table.fn.init = function () {
            table.render(_web_speech_api_manage.table.option.init);
        };

        _web_speech_api_manage.table.fn.init.success = function (data) {
        };

        _web_speech_api_manage.table.fn.init.error = function (xhr,status,error) {
        };

        // add
        _web_speech_api_manage.table.fn.add = function () {
            layer.open(_common.layerOption('xxx', [common.window.width + 'px', common.window.height + 'px'], 'xxx.html'));
        };

        _web_speech_api_manage.table.fn.add.success = function (data) {
        };

        _web_speech_api_manage.table.fn.add.error = function (xhr,status,error) {
        };

        // edit
        _web_speech_api_manage.table.fn.edit = function (obj) {
            layer.open(_common.layerOption('xxx', [common.window.width + 'px', common.window.height + 'px'], 'xxx.html#/id=' + obj.data.id));
        };

        _web_speech_api_manage.table.fn.edit.success = function (data) {
        };

        _web_speech_api_manage.table.fn.edit.error = function (xhr,status,error) {
        };

        // delete
        _web_speech_api_manage.table.fn.delete = function (obj) {
        };

        _web_speech_api_manage.table.fn.delete.success = function (data) {
            layer.msg('删除成功');
            $(".layui-form-checked").not('.header').parents('tr').remove();
        };

        _web_speech_api_manage.table.fn.delete.error = function (xhr,status,error) {
            layer.msg('删除失败');
        };

        // multi delete
        _web_speech_api_manage.table.fn.multiDelete = function (obj) {
        };

        /**
         * 【table 事件区】==================================================================================================
         */
        _web_speech_api_manage.table.event = {};

        _web_speech_api_manage.table.event.listener = function () {

            table.on('tool(xxx_table)', function (obj) {
                switch (obj.event) {
                    case 'edit_xxx':
                        _web_speech_api_manage.table.fn.edit(obj);
                        break;

                }
            });

            table.on('toolbar(xxx_table)', function(obj) {
                switch (obj.event) {
                    case 'import_xxx':
                        _web_speech_api_manage.table.fn.import(obj);
                        break;
                };
            });

        };

        /**
         * 【table 执行区】==================================================================================================
         */
        _web_speech_api_manage.table.fn.init();
        _web_speech_api_manage.table.event.listener();


        /**
         * 【form 定义区】==================================================================================================
         */
        _web_speech_api_manage.form = {};

        _web_speech_api_manage.form.option = {};

        /**
         * 【form 方法区】==================================================================================================
         */
        _web_speech_api_manage.form.fn = {};

        // init
        _web_speech_api_manage.form.fn.init = function () {
            form.render();
        };

        _web_speech_api_manage.form.fn.init.success = function (data) {
            form.val("tts_maker_form", data.data[0]);
        };

        _web_speech_api_manage.form.fn.init.error = function (xhr,status,error) {
        };

        // add
        _web_speech_api_manage.form.fn.add = function (data) {
            // xxx
            return false;
        };

        _web_speech_api_manage.form.fn.add.success = function (data) {
            layer.msg('添加成功');
        };

        _web_speech_api_manage.form.fn.add.error = function (xhr,status,error) {
            layer.msg('添加失败');
        };

        // read
        _web_speech_api_manage.form.fn.speech = function (data) {

            const chapters = data.field.text.split(/第.{1,9}章/);
            const chapterNames = data.field.text.match(/第.{1,9}章/g);
            _speech_define.fn.speech(data.field.voice, data.field.pitch, data.field.rate, data.field.volume, 0, chapters, chapterNames, "#chapter_name");
            return false;
        };

        _web_speech_api_manage.form.fn.speech.success = function (data) {
            layer.msg('添加成功');
        };

        _web_speech_api_manage.form.fn.speech.error = function (xhr,status,error) {
            layer.msg('添加失败');
        };

        // voice
        _web_speech_api_manage.form.fn.voice = function (data) {
            _speech_define.fn.voice("#voice");
            form.render();
        };

        _web_speech_api_manage.form.fn.voice.success = function (data) {
            layer.msg('添加成功');
        };

        _web_speech_api_manage.form.fn.voice.error = function (xhr,status,error) {
            layer.msg('添加失败');
        };

        /**
         * 【form 事件区】==================================================================================================
         */
        _web_speech_api_manage.form.event = {};

        _web_speech_api_manage.form.event.listener = function () {

            form.on('submit(voice_web_speech_api_form)', function (data) {
                _web_speech_api_manage.form.fn.voice(data);
                return false;
            });

            form.on('submit(speech_web_speech_api_form)', function (data) {
                _web_speech_api_manage.form.fn.speech(data);
                return false;
            });

            form.on('submit(pause_web_speech_api_form)', function (data) {
                _speech_define.fn.pause();
                return false;
            });

            form.on('submit(resume_web_speech_api_form)', function (data) {
                _speech_define.fn.resume();
                return false;
            });

            form.on('submit(cancel_web_speech_api_form)', function (data) {
                _speech_define.fn.cancel();
                return false;
            });

        };

        /**
         * 【form 执行区】==================================================================================================
         */
        _web_speech_api_manage.form.fn.init();
        _web_speech_api_manage.form.event.listener();

    })

})();